<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="教师名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="教师名称"></el-input>
    </el-form-item>
    <el-form-item label="教师头像" prop="img">
     <single-upload v-model="dataForm.img"></single-upload>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <!-- <el-input v-model="dataForm.sex" placeholder="性别"></el-input> -->
      <el-select v-model="dataForm.sex" clearable placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="籍贯" prop="nativePlace">
      <el-input v-model="dataForm.nativePlace" placeholder="籍贯"></el-input>
    </el-form-item>
    <el-form-item label="出生日期" prop="birthday">
      <!-- <el-input v-model="dataForm.birthday" placeholder="出生日期"></el-input> -->
      <el-date-picker
      v-model="dataForm.birthday"
      type="datetime"
      placeholder="出生日期时间">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="参加工作时间" prop="workTime">
      <!-- <el-input v-model="dataForm.workTime" placeholder="参加工作时间"></el-input> -->
      <el-date-picker
      v-model="dataForm.workTime"
      type="datetime"
      placeholder="参加工作日期时间">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="现从事专业" prop="profession">
      <el-input v-model="dataForm.profession" placeholder="现从事专业"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
  export default {
     components: { SingleUpload },
    data () {
      return {
         options: [{
          value: '男',
          label: '男'
        }, {
          value: '女',
          label: '女'
        } ],
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          img: '',
          sex: '',
          nativePlace: '',
          birthday: '',
          workTime: '',
          profession: ''
        },
        dataRule: {
          name: [
            { required: true, message: '教师名称不能为空', trigger: 'blur' }
          ],
          img: [
            { required: true, message: '教师头像不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          nativePlace: [
            { required: true, message: '籍贯不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '出生日期不能为空', trigger: 'blur' }
          ],
          workTime: [
            { required: true, message: '参加工作时间不能为空', trigger: 'blur' }
          ],
          profession: [
            { required: true, message: '现从事专业不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/teacherservice/teacherinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.teacherInfo.name
                this.dataForm.img = data.teacherInfo.img
                this.dataForm.sex = data.teacherInfo.sex
                this.dataForm.nativePlace = data.teacherInfo.nativePlace
                this.dataForm.birthday = data.teacherInfo.birthday
                this.dataForm.workTime = data.teacherInfo.workTime
                this.dataForm.profession = data.teacherInfo.profession
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/teacherservice/teacherinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'img': this.dataForm.img,
                'sex': this.dataForm.sex,
                'nativePlace': this.dataForm.nativePlace,
                'birthday': this.dataForm.birthday,
                'workTime': this.dataForm.workTime,
                'profession': this.dataForm.profession
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
